﻿using LyData.Data;
using System;
using System.Collections;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_MenuAdd : System.Web.UI.Page
{
    protected void Add_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            string str = this.MenuName.Text.ToString();
            string str2 = this.FileName.Text.ToString();
            string str3 = this.LocationList.SelectedItem.Value.ToString();
            string str4 = this.AttributerList.SelectedItem.Value.ToString();
            string str5 = this.Href.Text.ToString();
            string str6 = this.Encrypt.SelectedItem.Value.ToString();
            string str7 = this.Show.SelectedItem.Value.ToString();
            string str8 = this.TargetList.SelectedItem.Value.ToString();
            IDb db = new DbFactory().DriverDb();
            db.Open();
            string[,] includeColumns = new string[,] { 
                                { "MenuName", str, "NVarChar", "0" },
                                { "FileName", str2, "NVarChar", "32" },
                                { "Location", str3, "int", "0" }, 
                                { "Attributer", str4, "int", "0" },
                                { "Href", str5, "NvarChar", "0" }, 
                                { "Encrypt", str6, "int", "0" },
                                { "Show", str7, "int", "0" }, 
                                { "Target", str8, "Nvarchar", "32" }, 
                                { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" } 
            };
            db.Insert("menu", includeColumns);
            string str9 = db.TopRow("menuId", "menu", string.Format("FileName='{0}'", str2), "")[0].ToString();
            string str10 = str4;
            if (str10 != null)
            {
                string[,] strArray2;
                if (!(str10 == "0"))
                {
                    if (str10 == "1")
                    {
                        strArray2 = new string[,] { 
                                    { "ClassName", str, "NVarChar", "0" },
                                    { "FileName", str2, "NVarChar", "0" },
                                    { "MenuId", str9, "int", "0" },
                                    { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" }
                        };
                        db.Insert("ArticleClass", strArray2);
                    }
                    else if (str10 == "2")
                    {
                        strArray2 = new string[,] { 
                            { "ClassName", str, "NVarChar", "0" },
                            { "FileName", str2, "NVarChar", "32" }, 
                            { "MenuId", str9, "int", "0" },
                            { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" }
                        };
                        db.Insert("PicClass", strArray2);
                    }
                    else if (str10 == "3")
                    {
                        strArray2 = new string[,] { 
                                    { "ClassName", str, "NVarChar", "0" }, 
                                    { "FileName", str2, "NVarChar", "32" },
                                    { "MenuId", str9, "int", "0" }, 
                                    { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" }
                        };
                        db.Insert("ProductClass", strArray2);
                        string[,] strArray3 = new string[,] { { "MenuId", str9, "int", "0" } };
                        db.Insert("ProductConfig", strArray3);
                    }
                    else if (str10 == "4")
                    {
                        strArray2 = new string[,] { 
                                { "MenuName", str, "NVarChar", "0" },
                                { "FileName", str2, "NVarChar", "32" },
                                { "MenuId", str9, "int", "0" }, 
                                {"OrderId","0","int","0"},
                                { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" }
                        };
                        db.Insert("MessageConfig", strArray2);
                    }
                }
                else
                {
                    strArray2 = new string[,] { 
                        { "Title", str, "NVarChar", "0" }, 
                        { "FileName", str2, "NVarChar", "0" },
                        { "MenuId", str9, "int", "0" },
                         { "PageContent", "", "NVarChar", "0" }, 
                         { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" } };
                    db.Insert("SinglePage", strArray2);
                }
            }
            db.Close();
            this.Msg.Text = "栏目添加成功！";
            this.Msg.Visible = true;
            this.AttributerList.SelectedIndex = -1;
            this.LocationList.SelectedIndex = -1;
            this.MenuName.Text = "";
            this.Href.Text = "";
            this.FileName.Text = "";
            this.ContentMenu.Bind();
        }
    }

    protected void Bind()
    {
        MenuProvider provider = new MenuProvider();
        this.LocationList.DataSource = provider.Location;
        this.LocationList.DataValueField = "key";
        this.LocationList.DataTextField = "value";
        this.LocationList.DataBind();
        SortedList attributer = provider.Attributer;
        attributer.Remove("900");
        this.AttributerList.DataSource = attributer;
        this.AttributerList.DataValueField = "key";
        this.AttributerList.DataTextField = "value";
        this.AttributerList.DataBind();
        this.TargetList.DataSource = provider.Target;
        this.TargetList.DataValueField = "key";
        this.TargetList.DataTextField = "value";
        this.TargetList.DataBind();
        this.TargetList.SelectedIndex = 1;
    }

    protected void FileNameCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        int num = db.Scalar("count(FileName)", "menu", string.Format("FileName='{0}'", args.Value.ToString()));
        db.Close();
        if (num > 0)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.Visible = false;
        if (!ManagerLogin.ValidateRole("B_Website.aspx"))
        {
            this.Msg.CssClass = "errorMsg";
            this.Msg.Text = "您无此权限！";
            this.Add.Enabled = false;
            this.Msg.Visible = true;
        }
        else
        {
            if (!base.IsPostBack)
            {
                this.Bind();
            }
            for (int i = 0; i < this.AttributerList.Items.Count; i++)
            {
                this.AttributerList.Items[i].Attributes.Add("onclick", "radioAttributer(this)");
            }
        }
    }
}
